package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.activities.ChooseInfoTypeActivity;
import com.zadtek.shopmall.activities.MyInfoListActivity;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.InfoType;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.GridSpacingItemDecoration;

import java.util.LinkedList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zack on 2018/5/27.
 */

public class PublishInfoFragment extends AbsFragment {
    private RecyclerView recyclerView;
    private TextView textView;
    private PublishInfoAdapter adapter ;
    private List<InfoType> data = new LinkedList<>();

    @Override
    public int setView() {
        return R.layout.fragment_publish_info;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        NetTool.getApi().getInfoType().observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<BaseResponse<List<InfoType>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<InfoType>> value) {
                        if(value.getCode() == 0 && value.getData() != null){
                            if(value.getData().size()!=0) {
                                data.addAll(value.getData());
                                adapter.notifyDataSetChanged();
                            }else{
                                ToastUtil.showToast("未获取到分类信息！");
                            }
                        }
                    }
                });
    }

    private void initLogic() {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mActivity, MyInfoListActivity.class));
                ToastUtil.showToast("发布服务！");
            }
        });
        adapter = new PublishInfoAdapter(R.layout.type_item,data);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(mActivity,ChooseInfoTypeActivity.class).putExtra("data", data.get(position)));
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity,3);
        recyclerView.setLayoutManager(gridLayoutManager);
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(3,getResources().getDimensionPixelSize(R.dimen.normal_margin),true);
        recyclerView.addItemDecoration(decoration);
        textView = (TextView) findViewById(R.id.tv_my_publish);
    }

    private class PublishInfoAdapter extends BaseQuickAdapter<InfoType,BaseViewHolder>{
        public PublishInfoAdapter(int layoutResId, @Nullable List<InfoType> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, InfoType item) {
            helper.setText(R.id.text,item.getName());
            Glide.with(mActivity).load(item.getCover()).into((ImageView) helper.getView(R.id.img));
        }
    }
}
